@ECHO off

REM Setting up devenv path
echo Checking MSVC version

SET MSVCROOTKEY=HKLM\SOFTWARE\Microsoft\VisualStudio
SET MSVC71KEY=%MSVCROOTKEY%\7.1\Setup\VC
SET MSVC8KEY=%MSVCROOTKEY%\8.0\Setup\VC
SET MSVC8EXPRESSKEY=HKLM\SOFTWARE\Microsoft\VCExpress\8.0\Setup\VC

REM First see if we can find MSVC, then set the variable
REM NOTE: delims=<tab><space>
REM NOTE: run the initial REQ QUERY outside of the if() to set ERRORLEVEL correctly

REG QUERY "%MSVC71KEY%" /v ProductDir >nul 2>nul
if "%VC71DIR%"=="" (
  IF %ERRORLEVEL% EQU 0 (
    FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%MSVC71KEY%" /v ProductDir') DO SET VC71DIR=%%B
  )
)

REG QUERY "%MSVC8KEY%" /v ProductDir >nul 2>nul
if "%VC8DIR%"=="" (
  IF %ERRORLEVEL% EQU 0 (
    FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%MSVC8KEY%" /v ProductDir') DO SET VC8DIR=%%B
  )
)

REG QUERY "%MSVC8EXPRESSKEY%" /v ProductDir >nul 2>nul
if "%VC8EXPRESSDIR%"=="" (
  IF %ERRORLEVEL% EQU 0 (
    FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%MSVC8EXPRESSKEY%" /v ProductDir') DO SET VC8EXPRESSDIR=%%B
  )
)

if not "%VC8EXPRESSDIR%"=="" (
  set VCDIR=%VC8EXPRESSDIR%
  set PROJ=quake3\code\win32\msvc8
)
if not "%VC71DIR%"=="" (
  set VCDIR=%VC71DIR%
  set PROJ=quake3\code\win32\msvc
)
if not "%VC8DIR%"=="" (
  set VCDIR=%VC8DIR%
  set PROJ=quake3\code\win32\msvc8
)

echo Set devenv path.
call "%VCDIR%\Bin\vcvars32.bat"

echo Building Project.
devenv "%PROJ%\quake3.sln" /build release


REM Making Bin directory

echo Making sure bin dir exists...

IF NOT EXIST bin\NUL md bin

echo Copying executable and dlls...
copy /Y %PROJ%\build\quake3_release\ioquake3.exe bin\ioquake3.exe
copy /Y %PROJ%\build\game_release\qagamex86.dll bin\qagamex86.dll
copy /Y %PROJ%\build\cgame_release\cgamex86.dll bin\cgamex86.dll
copy /Y %PROJ%\build\q3_ui_release\uix86.dll bin\uix86.dll

echo Making sure genetic dir exists...
IF NOT EXIST bin\genetic\NUL md bin\genetic

echo Making sure baseq3 dir exists...
IF NOT EXIST bin\baseq3\NUL md bin\baseq3

echo Copying dlls to baseq3 and genetic...
copy /Y bin\*.dll bin\genetic\
copy /Y bin\*.dll bin\baseq3\

echo Copying genetic files to genetic...
xcopy /S /E /Y resources\genetic\* bin\genetic\
xcopy /S /E /Y resources\genetic\* bin\baseq3\

echo Copying run script...
copy /Y resources\run.bat bin\run.bat

IF NOT EXIST bin\baseq3\pak0.pk3 echo Don't forget to copy the pak files to bin\baseq3.

